package com.tensquare.spit.service;

import com.tensquare.entity.IdWorker;
import com.tensquare.entity.PageResult;
import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author zzb
 * @version 1.0
 * @create 2020/12/17 11:52
 */
@Service
public class SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    //查询全部
    public List<Spit> findAll() {
        return spitDao.findAll();
    }

    //添加吐槽
    public void add(Spit spit) {
        //设置id
        spit.set_id(idWorker.nextId()+"");
        spitDao.save(spit);
    }

    //根据id查询吐槽成功
    public Spit findById(String spitId) {
        return spitDao.findById(spitId).get();
    }

    //修改吐槽
    public void update(String spitId, Spit spit) {
        //把id和对象绑定
        spit.set_id(spitId);
        //修改
        spitDao.save(spit);
    }

    //根据id删除吐槽
    public void delete(String spitId) {
        spitDao.deleteById(spitId);
    }

    //根据上级查询吐槽列表
    public PageResult<Spit> comment(String parentid, Integer page, Integer size) {
        Page<Spit> spitPage = spitDao.findByParentid(parentid, PageRequest.of(page - 1, size));
        return new PageResult<>(spitPage.getTotalElements(),spitPage.getContent());
    }

    /**
     * 点赞处理(传统方法)
     * @param spitId    需要点赞的吐槽用户的id
     * @return          是否点赞成功 true：点赞成功 false点赞失败
     */
    public boolean thumbup(String spitId) {
        //从redis中得到一个第一次点赞成功放入的数据，如果存在证明点赞过
        Object object = redisTemplate.opsForValue().get("thumbup_" + spitId);
        if (object == null){    //此时代表未曾点赞过
            //根据id查询点赞对象
            Spit spit = spitDao.findById(spitId).get();
            //修改点赞加一
            spit.setThumbup(spit.getThumbup()+1);
            //保存
            spitDao.save(spit);
            //保存到redis中
            redisTemplate.opsForValue().set("thumbup_"+spitId,"test");
            return true;
        }

        return false;
    }

    /**
     * 点赞处理（利用mongoTemplate处理）第二种方法
     * @param spitId
     * @return
     */
    public boolean thumbup2(String spitId){
        //从redis中得到一个第一次点赞成功后放入的数据，如果存在证明点赞过
        Object object = redisTemplate.opsForValue().get("thumbup001_" + spitId);
        if (object == null){        //证明没有点赞过
            //定义查询条件
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spitId));
            //定义修改的值对象
            Update update = new Update();
            update.inc("thumbup",1);
            mongoTemplate.updateFirst(query,update,"spit");//参数三代表修改的集合名称
            //保存到redis中
            redisTemplate.opsForValue().set("thumbup001_"+spitId,"aaa");
            return true;
        }
        return false;
    }
}
